class Solution {
    func romanToInt(_ s: String) -> Int {
        let valueDic: [Character: Int] = ["I": 1,"V": 5,"X": 10,"L": 50,"C": 100,"D": 500,"M": 1000,]
        var total = 0
        var deviation = 0
        s.forEach { (char) in
            let value = valueDic[char] ?? 0
            total += value
            if deviation < value{
                total -= 2 * deviation
            }
            deviation = value
        }
        return total
    }
}
